import scrapy
from scrapy import signals
from scrapy.http import  Response
'''
 Selenium 和 Scrapy的结合
'''
from selenium import webdriver

class GuaziSpider(scrapy.Spider):
    name = 'guazi'
    allowed_domains = ['guazi.com']
    start_urls = ['https://www.guazi.com/sh/buy']

    custom_settings = {
        'ITEM_PIPELINES': {'scrapy_demo.pipelines.Doubanipeline': 100, },
        'DOWNLOADER_MIDDLEWARES': { 'scrapy_demo.middlewares.SeleniumMiddleware': 300 }
    }

    #
    def parse(self, response: Response):
        print(response.body)
        a = 1
        b = 2
        pass


    @classmethod
    def from_crawler(cls, crawler, *args, **kwargs):
        spider = super(GuaziSpider, cls).from_crawler(crawler, *args, **kwargs)
        # 添加对象
        chrome_driver = '/Users/edgar/chromedriver'
        spider.chrome = webdriver.Chrome(executable_path = chrome_driver)
        crawler.signals.connect(spider.spider_closed, signal=signals.spider_closed) # 爬虫结束后
        return spider


    def spider_closed(self, spider):
        spider.logger.info('Spider closed: %s', spider.name)
        spider.chrome.quit()
